<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-witth="120px" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="6" >
                <el-form-item  label="用  户  名:"  label-width="120px"  label-position="left"   :required="true"  prop="username" >
                    <el-input v-model="postForm.username"  :maxlength="50"   />
                </el-form-item>
          </el-col>
        </el-row>

        <el-row>
            <el-col :span="6" >
                <el-form-item  label-width="120px"  label-position="left"  :required="true"   label="密  码:"   prop="actionPassword" >
                    <el-input  v-model="postForm.actionPassword" type="password"  :maxlength="50"   />
                </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6" >
            <el-form-item  label="国际区号:"  label-width="120px"  label-position="left"  prop="area_code"  >
              <el-input v-model="postForm.area_code"  :maxlength="50" required />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6" >
              <el-form-item  label="手机号码:"  label-width="120px"  label-position="left"  prop="mobile"  >
                <el-input v-model="postForm.mobile"  :maxlength="50"  />
              </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item  label-width="120px"  label-position="left"   label="一级仓库 :" prop="sid" :required="true">
              <el-select v-model="postForm.sid"  placeholder="请选择一级仓库"  @change="changeStorage">
                <el-option :label="item.storageName" :value="item.id" v-for="(item,index) of storageListArr" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item  v-model="postForm.rid"   label-width="120px"  label-position="left"   label="二级仓库 :" prop="rid" :required="true">
              <el-select v-model="postForm.rid" label="二级仓库 :" placeholder="请选择二级仓库">
                <el-option :label="item.roomName" :value="item.id" v-for="(item , index) of roomListArr" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
                <el-form-item  label=" 类 型 :"  label-width="120px"  prop="position" :required="true">
                    <el-select v-model="postForm.position" placeholder="请选择用户类型">
                        <el-option label="管理员" value="1"></el-option>
                        <el-option label="技术员" value="2"></el-option>
                        <el-option label="租户"   value="3"></el-option>
                    </el-select>
                </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="2">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
          </el-col>
          <el-col :span="2">
            <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
          </el-col>

        </el-row>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
//import { validateURL } from '@/utils/validate'
import { createStorage } from '@/api/storageuser'
import {fetchList} from  '@/api/storage'
import { roomList } from  '@/api/room'
import { userSearch } from '@/api/remoteSearch'
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'

const defaultForm = {
  sid: '', // 一级仓库ID号,storageId
  rid: '', // 二级仓库ID号,roomId
  username: '', // 用户名,userId
  mobile: '', // 用户手机号码
  actionPassword: '', // 当前操作用户的操作密码
  uid: 1,  // userId
  password: '', //新用户的登录密码 如果不输入，默认是： 123456
  position:''  //1管理员,2技术员,3租户
}

export default {
  name: 'StorageuserAdd',
  components: { Tinymce, MDinput, Upload, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        // this.$message({
        //   message: rule.field + '为必传项!',
        //   type: 'error'
        // })
        if(rule.field == 'username'){
          callback(new Error('用户名为必传项！'))
        }
        // if(rule.field == 'mobile'){
        //   callback(new Error('手机号码为必传项！'))
        // }
        if(rule.field == 'position'){
          callback(new Error('用户类型为必传项!'))
        }else{
          callback(new Error(rule.field + '为必传项!'))
        }

      } else {
        callback()
      }
    }

    return {
      selectedValue : 119 ,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      selectedCustom: '',
      selectedRoom: '',
      selectedStorage: '',
      userListOptions: [] ,
      storageListArr: [] ,
      roomListArr: [] ,
      rules: {
        username: [{ validator: validateRequire }] ,
        sid: [{ validator: validateRequire }] ,
        rid: [{ validator: validateRequire }],
        mobile: [{ validator: validateRequire }] ,
        actionPassword: [{ validator: validateRequire }] ,
        position: [{ validator: validateRequire }] ,
      }
    }
  },
  computed: {
  },
  created() {
    fetchList({ "uissssd" : 1269 , "page": 1, "limit" : 5000 , "status" : 0 } ).then(response => {
      this.storageListArr = response.data.data
    })

    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id

    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    changeStorage(val){
      let changedStorageId = this.postForm.sid;
      this.postForm.rid = '';
      // for( let i = 0 ; i < this.storageListArr.length ; i++){
      //   if( this.storageListArr[i]['storageName'] == val){
      //     changedStorageId = this.storageListArr[i]['id']
      //   }
      // }
      console.log(val)
      this.roomListArr = []
      //this.postForm.storage_name = this.selectedStorage = val
      this.fetchRoomData(changedStorageId)
    },
    fetchRoomData(sid){

      roomList({ page: 1, limit: 1000, sid:sid }).then(response => {
        this.roomListArr = response.data.data

      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      //this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    doSubmitForm() {
      createStorage(this.postForm).then(response => {
        if(response.data.code == 0){
           this.$message({
            message:  '保存成功!',
            type: 'success'
          })
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
.el-form-item__label {
  text-align: left;
}
</style>
